<?php

require_once __DIR__.'/vendor/autoload.php';

//// 创建一个Worker监听2345端口，使用http协议通讯
$worker = new \Workerman\Worker("http://0.0.0.0:2345");

// 启动进程数
$worker->count = 1;

// 当连接时
$worker->onConnect = function(\Workerman\Connection\TcpConnection $connection)
{
    echo "new connection from ip:{$connection->getRemoteIp()}\n";
};

//接收到浏览器发送的数据时回复hello world给浏览器
$worker->onMessage = function(\Workerman\Connection\TcpConnection $connection, \Workerman\Protocols\Http\Request $request)
{
    $connection->send('hello world');
};

// 当连接关闭时，http没有onclose
$worker->onClose = function(\Workerman\Connection\TcpConnection $connection)
{
    echo "connection closed\n";
};


//所有子进程初始化
$worker->onWorkerStart = function($worker){
    echo "Worker starting\n";
};

// 设置reloadable为false，即子进程收到reload信号不执行重启 php index.php reload 命令
//$worker->reloadable = false;
// 执行reload后告诉所有客户端服务端执行了reload
$worker->onWorkerReload = function(\Workerman\Worker $worker)
{
    foreach($worker->connections as $connection)
    {
        $connection->send('worker reloading');
    }
};

//运行所有Worker实例
//windows版本的workerman不支持在同一个文件中实例化多个Worker。
\Workerman\Worker::runAll();